import MinerHome from "../../funcObj/interface/MinerHome";
import GameManager from "../../manager/GameManager";
import NodePoolManager from "../../manager/NodePoolManager";
import Player from "../../player/Player";
import DestructibleFacility from "../DestructibleFacility";
import PlayerManager from "../../manager/PlayerManager";
import { Killer } from "../../funcObj/interface/HealthPoint";
import Miner from "../../flyer/Other/Miner";

const { ccclass, property } = cc._decorator;

/**
 * 矿机 生成6个矿工
 */
@ccclass
export default class MiningMachine extends DestructibleFacility implements MinerHome {
    minerArr: Miner[];

    @property(cc.SpriteFrame)
    color: cc.SpriteFrame[] = [];

    init(player: Player): void {
        super.init(player);
        this.node.getChildByName("color").getComponent(cc.Sprite).spriteFrame = this.color[player.playerId];
        this.minerArr = [];
        cc.tween(this.node)
            .call(this.addMiner.bind(this))
            .delay(0.5)
            .union()
            .repeat(6)
            .start();
    }

    addMineCount(carryCount: number): void {
        this.player.diamond += carryCount;
        //播放获得矿石的动画
        this.player == PlayerManager.instance.controllerPlayer && GameManager.instance.showResourceTips(1, carryCount, this.node.position);
    }

    addMiner(): void {
        let miner = NodePoolManager.instance.obtain(NodePoolManager.instance.minerPrefab);
        let minerComp = miner.getComponent(Miner);
        miner.position = this.node.position;
        miner.parent = GameManager.instance.minerParent;
        miner.getComponent(Miner).init(this);
        this.minerArr.push(minerComp);
    }

    die(killer: Killer): void {
        super.die(killer);
        this.node.stopAllActions();
        this.minerArr.forEach(item => item.die())
        this.minerArr = null;
    }
}
